package fule.com.playandroidkotlin.ui.activity

import android.content.Intent
import android.view.View.GONE
import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener
import fule.com.playandroidkotlin.base.BaseActivity
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.Model
import fule.com.playandroidkotlin.ui.adapter.ArticleListAdapter
import fule.com.playandroidkotlin.ui.adapter.ArticleTabAdapter
import fule.com.playandroidkotlin.ui.model.ArticleListModel
import fule.com.playandroidkotlin.ui.model.ArticlesBean
import fule.com.playandroidkotlin.ui.model.ChildrenBean
import fule.com.playandroidkotlin.ui.presenter.ArticleListPresenter
import fule.com.playandroidkotlin.ui.util.AppConstant
import fule.com.playandroidkotlin.ui.view.ArticleView
import fule.com.playandroidkotlin.ui.weight.DividerDecoration
import kotlinx.android.synthetic.main.activity_knowledge.*
import kotlinx.android.synthetic.main.toolbar.*

@Suppress("CAST_NEVER_SUCCEEDS")
/**
 * 作者： njb
 * 时间： 2018/8/11 0011-下午 3:26
 * 描述：
 * 来源：
 */
class KnowledgeActivity : BaseActivity<ArticleListPresenter>(), (ArticleView) {
    private lateinit var articleAdapter:ArticleListAdapter
    private var mList : MutableList<ArticleListModel.DatasBean> ?= null
    private var cid:Int = 0
    private var titles = ""
    private var articleTabAdapter: ArticleTabAdapter? = null
    private var knowledge: MutableList<ChildrenBean>? = null
    private var page = 0

    override val layoutId: Int
        get() = R.layout.activity_knowledge

    override fun createPresenter(): ArticleListPresenter {
        return ArticleListPresenter(this)
    }

    override fun initView() {
        initTabAdapter()
        initIntent()
        initToolBar()
        initAdapter()
    }

    private fun initIntent() {
        if (intent != null && intent.extras != null) {
            intent.extras.let {
                cid = intent.extras!!.getInt(AppConstant.ARTICLELIST_ID)
                titles = intent.extras!!.getString(AppConstant.ARTICLELIST_TITLE)
                knowledge = intent.extras!!.getParcelableArrayList(AppConstant.CONTENT_DATA_KEY)
            }
            articleTabAdapter!!.setNewData(knowledge)
            knowledge!![0].isSelect = true
            presenter!!.articleList(page, cid)
        }
    }

    private fun initToolBar() {
        toolbar.run {
            toolbar.title = titles
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
            iv_search.visibility = GONE
        }
    }

    private fun initTabAdapter() {
        articleTabAdapter = ArticleTabAdapter(null)
        articleTabAdapter.run {
            var manager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            rv_tab.layoutManager = manager
        }
        rv_tab.run {
            rv_tab.addItemDecoration(DividerDecoration( ContextCompat.getColor(context,R.color.ce7e7e7), 1))
            rv_tab.adapter = articleTabAdapter
        }
    }

    private fun initAdapter() {
        articleAdapter = ArticleListAdapter(null)
        articleAdapter.run {
            val manager = LinearLayoutManager(context)
            rv_knowledge.layoutManager = manager
/*            articleAdapter!!.bindToRecyclerView(rv_knowledge)
            articleAdapter!!.setEmptyView(R.layout.empty_layout_no_data)*/
        }

        rv_knowledge.run {
            rv_knowledge.addItemDecoration(DividerDecoration(ContextCompat.getColor(context,R.color.ce7e7e7), 1))
            rv_knowledge.adapter = articleAdapter
        }

        articleAdapter!!.setOnItemClickListener { _, _, position ->
            Intent(context, WebViewActivity::class.java).run {
                putExtra(AppConstant.WEBVIEW_URL_KEY, articleAdapter!!.data[position].link)
                putExtra(AppConstant.WEBVIEW_TITLE_KEY, articleAdapter!!.data[position].title)
                putExtra(AppConstant.WEBVIEW_ID_KEY, articleAdapter!!.data[position].id)
                startActivity(this)
            }
        }
    }

    override fun addListener() {

        rf_knowledge.setOnRefreshListener {
            page
            presenter!!.articleList(page, cid)
        }
        rf_knowledge.setOnLoadMoreListener {
            page++
            presenter!!.articleList(page, cid)
        }

        articleTabAdapter!!.onItemClickListener = OnItemClickListener { _, _, position ->
            var data = articleTabAdapter!!.data
            for (m in data) {
                m.isSelect = false
            }
            data[position].isSelect = true
            cid = articleTabAdapter!!.data[position].id
            articleTabAdapter!!.notifyDataSetChanged()
            page = 0
            presenter!!.articleList(0, cid)
        }
    }

    override fun showLoading() {
        showWaitDialog()
    }

    override fun hideLoading() {
        dismissWaitDialog()
    }

    override fun onErrorCode(model: Model) {
        super.onErrorCode(model)
        rf_knowledge.finishLoadMore(300)
        rf_knowledge.finishRefresh(300)
    }

    override fun onLoadArticleList(data: ArticleListModel) {
        mList = data.datas

        if (page == 0) {
            articleAdapter!!.setNewData(data.datas)
        } else {
            articleAdapter!!.addData(data.datas!!)
        }
        rf_knowledge.finishRefresh(300)
        rf_knowledge.finishLoadMore(300)
    }
}

